
package com.zlt.controller.recruiter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.zlt.controller.BaseController;
import com.zlt.dao.Impl.JobDaoImpl;
import com.zlt.dao.JobDao;
import com.zlt.dto.DeliveryDTO;
import com.zlt.dto.JobDTO;
import com.zlt.entity.Company;
import com.zlt.entity.JobType;
import com.zlt.entity.OnlineResume;
import com.zlt.entity.Resume;
import com.zlt.service.DeliveryService;
import com.zlt.service.JobService;
import com.zlt.service.impl.DeliveryServiceImpl;
import com.zlt.service.impl.JobServiceImpl;
import com.zlt.util.ActiveUserUtil;
import com.zlt.util.ClassUtil;
import com.zlt.vo.*;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author IBing
 * @Date 2022/8/11 13:03
 * @Version 1.0
 */

@WebServlet("/recruiter/delivery/*")
public class DeliveryController extends BaseController {

    private DeliveryService deliveryService = new DeliveryServiceImpl();
    private JobService jobService = new JobServiceImpl();

    @Override
    protected void defaultMethod(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    /**
     * 展示提交的简历列表
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected Result showJobResume(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        DeliveryDTO deliveryDTO = ClassUtil.resolveRequest(DeliveryDTO.class,request);
        System.out.println(deliveryDTO.getSearchParams());
        try {
            Pager<DeliveryVO> pager = new Pager<>();
            pager.setPage(deliveryDTO.getPage());
            pager.setLimit(deliveryDTO.getLimit());
            pager = deliveryService.selectJobResume(pager,deliveryDTO);

            return  Result.ok("查找成功",pager) ;
        }catch (RuntimeException e){
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 批量处理通过
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected Result batchPass(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String data = request.getParameter("data");
        JSONArray objs = JSON.parseArray(data);
        System.out.println(objs);
        List didList=new ArrayList();
        for (Object obj : objs) {
            JSONObject object =  null;
            try{
                object=JSONObject.parseObject(String.valueOf(obj));
            }catch (JSONException e){
                e.printStackTrace();
            }
            //获取did
            String did =object.getString("did");
            didList.add(did);
        }
        System.out.println(didList);

        try {
            boolean success = deliveryService.updateResumeState(didList,1);
            return success ? Result.ok("批量处理成功") : Result.error("批量处理失败");
        }catch (RuntimeException e){
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 批量处理拒绝
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected Result batchReject(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String data = request.getParameter("data");
        JSONArray objs = JSON.parseArray(data);
        System.out.println(objs);
        List didList=new ArrayList();
        for (Object obj : objs) {
            JSONObject object =  null;
            try{
                object=JSONObject.parseObject(String.valueOf(obj));
            }catch (JSONException e){
                e.printStackTrace();
            }
            //获取did
            String did =object.getString("did");
            didList.add(did);
        }
        System.out.println(didList);

        try {
            boolean success = deliveryService.updateResumeState(didList,2);
            return success ? Result.ok("批量处理成功") : Result.error("批量处理失败");
        }catch (RuntimeException e){
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 简历筛选通过
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected Result passJobResume(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String data=request.getParameter("data");
        JSONObject object =  null;
        try{
            object=JSONObject.parseObject(data);
        }catch (JSONException e){
            e.printStackTrace();
        }
        //获取did
        String did =object.getString("did");
        //获取用户信息
//        String u =object.getString("user");
//        JSONObject jsondata= JSON.parseObject(u);
//        String uid = jsondata.getString("uid");
        System.out.println(did);

        try {
            boolean success = deliveryService.updateResumeState(Integer.valueOf(did),1);
            return success ? Result.ok("通过成功") : Result.error("通过失败");
        }catch (RuntimeException e){
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 简历筛选不通过
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected Result rejectJobResume(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String data=request.getParameter("data");
        JSONObject object =  null;
        try{
            object=JSONObject.parseObject(data);
        }catch (JSONException e){
            e.printStackTrace();
        }
        //获取did
        String did =object.getString("did");

        System.out.println("did:"+did);

        try {
            boolean success = deliveryService.updateResumeState(Integer.valueOf(did),2);
            return success ? Result.ok("拒绝成功") : Result.error("拒绝失败");
        }catch (RuntimeException e){
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 查看简历
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected Result onlineResume(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取前端传过来的数据
        String data=request.getParameter("data");
        //解析成json对象
        JSONObject object =  null;
        try{
            object=JSONObject.parseObject(data);
        }catch (JSONException e){
            e.printStackTrace();
        }
        //获取用户信息
        String u =object.getString("user");
        JSONObject jsondata= JSON.parseObject(u);
        String uid = jsondata.getString("uid");
        System.out.println("uid:==="+uid);
        try {
            Resume resume = deliveryService.onlineViewResume(uid);
            return Result.ok("查看成功",resume.getPath());
        }catch (RuntimeException e){
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 查询岗位分类信息
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    protected Result jobType(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        List<JobType> jobTypeList = jobService.selectJobTypeByCid();
        return Result.ok("查询分类成功",jobTypeList);
    }

}
